/*
 * Copyright (c) 2025 [ByteDance Ltd. and/or its affiliates.] and/or its affiliates
 * SPDX-License-Identifier: LGPL v2.1+
*/
#include "libavcodec/codec.h"
#if defined(_WIN32)
#include <Windows.h>

typedef void* (*funcDef)(const char*, int encoder);

const AVCodec* find_encoder_for_proxy(const char* name) {
    const AVCodec* codec = avcodec_find_encoder_by_name(name);
    if (codec) {
        return codec;
    }
    static HMODULE vesdk_lib = NULL;
    static funcDef find_codec = NULL;
    static const wchar_t* hLibs[] = {L"ttvecodec.dll", L"ttvideoeditor.dll", L"ttvebase.dll"};
    int i = 0;
    for (i = 0; i < _countof(hLibs); ++i) {
        if (NULL == vesdk_lib)
            vesdk_lib = LoadLibraryW(hLibs[i]);
        if (vesdk_lib) {
            if (NULL == find_codec)
                find_codec = (funcDef)GetProcAddress(vesdk_lib, "vesdk_find_codec_by_name");
            if (find_codec) {
                return (const AVCodec*)find_codec(name, 1);
            }
            FreeLibrary(vesdk_lib);
            vesdk_lib = NULL;
        }
    }
    
    return NULL;
}

typedef void* (*funcCreateDef)(void *);

void* create_de_hdr_banding_instance(void *avctx) {
    static HMODULE vesdk_lib = NULL;
    if (NULL == vesdk_lib)
        vesdk_lib = LoadLibraryW(L"ttvideoeditor.dll");
    if (vesdk_lib) {
        static funcCreateDef create_inst = NULL;
        if (NULL == create_inst)
            create_inst = (funcCreateDef)GetProcAddress(vesdk_lib, "vesdk_create_de_hdr_banding_instance");
        if (create_inst) {
            void* inst = create_inst(avctx);
            return inst;
        }
    }
    return NULL;
}

#else
const AVCodec* find_encoder_for_proxy(const char* name) {
    return avcodec_find_encoder_by_name(name);
}

void* create_de_hdr_banding_instance(void* avctx) {
    return NULL;
}
#endif
